package com.vip.base.controller;


import com.vip.base.service.ISysPermService;
import com.vip.common.annotation.LogCollection;
import com.vip.common.constant.UrlConsts;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.database.dto.perm.AddPermDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 权限表 前端控制器
 *
 * @author LEON
 * @since 2020-03-28
 */
@RestController
@RequestMapping(UrlConsts.API + "/perm")
public class SysPermController {

    @Autowired
    private ISysPermService sysPermService;

    /**
     * 添加新权限
     *
     * @param dto
     * @param bindingResult
     * @return
     */
    @LogCollection
    @PreAuthorize("hasAuthority('perm:add')")
    @PostMapping
    public RestResponse<String> addNewPermission(@RequestBody @Valid AddPermDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResponses.newFailResponse(ErrorCode.INVALID_PARAMETER, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RestResponses.newResponseFromResult(sysPermService.addNewPerm(dto));
    }

}

